package com.paipai.controller;

import com.paipai.Vo.MyOrderVo;
import com.paipai.feign.OrderFeign;
import com.paipai.utils.CookieUtils;
import com.paipai.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderFeign orderFeign;

    //选择查询
    @RequestMapping("/selectOrders")
    @ResponseBody
    public List<MyOrderVo> select(Long buyerId, Integer state) {
        return orderFeign.selectOrder(buyerId, state);
    }
    //支付订单
    @RequestMapping("/pay")
    @ResponseBody
    public String pay(HttpServletRequest request,Long productId, Integer state){
        String token = CookieUtils.getCookie("token", request);
        String name = JwtUtils.getAccount(token);
        Integer pay = orderFeign.pay(name,productId, state);
        if (1==pay){
            //支付成功
            return "success";
        }else {
            //支付失败
            return "failure";
        }
    }

    //通过名字查询
    @RequestMapping("/selectMyOrder")
    @ResponseBody
    public List<MyOrderVo> selectMyOrder(HttpServletRequest request, Integer state) {
        String token = CookieUtils.getCookie("token", request);
        String name = JwtUtils.getAccount(token);
        /*System.out.println(name);
        System.out.println("____________________________________");
        System.out.println(orderFeign.selectMyOrder(request,state));*/

        return orderFeign.selectMyOrder(name,state);
    }
}
